package com.ruoyi.system.outto.goods.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttrTemplateMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.interAttr.attr.NiGuangAttr;
import com.ruoyi.system.interAttr.domain.InterAttr;
import com.ruoyi.system.interAttr.mapper.InterAttrMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttrTemplate;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.goods.NIGUANGAPI;
import com.ruoyi.system.outto.utils.GoodsInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class NIGUANGApiImpl implements NIGUANGAPI {
    
    private static String ATTR_NAME = "name";
    private static String ATTR_VALUE = "value";

    @Resource
    InterAttrMapper interAttrMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    private GameAttrTemplateMapper gameAttrTemplateMapper;
    @Autowired
    private NiGuangAttr niGuangAttr;
    @Autowired
    private GoodsInfoUtils goodsInfoUtils;

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        TreeMap<String, Object> treeMap = builderParam(goodsInfoAndAttrValueDto, salePartner);
        HashMap<Object, Object> result = new HashMap<>();

        String goods_add = niGuangAttr.post("goods_add", treeMap, salePartner);
        goods_add = MapUntils.unicodeToString(goods_add);
        JSONObject response = JSONObject.parseObject(goods_add);

        if (!"1000".equals(response.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
            JSONObject data = response.getJSONObject("data");
            String outId = data.getString("id");
            result.put("outId", outId);
        }
        log.info("看看账号添加请求参数={}", JSONObject.toJSONString(treeMap));
        log.info("看看账号添加返回参数={}", response);
        result.put("req", JSONObject.toJSONString(treeMap));
        result.put("res", goods_add);
        result.put("msg", response.getString("msg"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> result = new HashMap<>();
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        String outId = artRecs.get(0).getOutid();

        // 商品基础信息
        TreeMap<String, Object> good_info_map = new TreeMap<>();
        // 商品id
        good_info_map.put("goods_id", outId);
        // 商品名称
        good_info_map.put("goods_name", goodsInfoAndAttrValueDto.getTitle());
        // 商品价格,1-99999
        good_info_map.put("goods_price", goodsInfoAndAttrValueDto.getPrice());
        // 商品图片
        good_info_map.put("goods_banner", goodsInfoUtils.getGoodsInfoImageList(goodsInfoAndAttrValueDto));
        // 修改商品基础信息
        String response = niGuangAttr.post("goods_edit", good_info_map, salePartner);

        /**
         * 逆光商品不修改商品属性
         * 原因：商品属性只能单个修改，如果商品中添加了新的皮肤或者英雄，无法知道对应的哪个属性修改了
         *      如果需要修改则需要遍历所有属性，由于远程推送，遍历会导致不可控的因素，进而不修改属性
         */
        /*// 商品属性信息(区服，单选，多选)
        TreeMap<String, Object> good_attr_map = (TreeMap<String, Object>) treeMap.get("attr_obj");
        good_attr_map.put("goods_id", outId);
        // 修改商品属性信息
        String goods_attr_response = niGuangAttr.post("goods_edit_attr", good_attr_map, salePartner);*/


        // 将参数转为 Json
        JSONObject goodsJson = JSONObject.parseObject(response);
        if (StringUtils.equals(goodsJson.getString("code"), "1000")) {
            result.put("code", 200);
        } else {
            result.put("code", 500);
        }
        result.put("req", JSONObject.toJSONString(good_info_map));
        result.put("res", response);
        result.put("msg", goodsJson);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> result = new HashMap<>();

        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        String outId = artRecs.get(0).getOutid();

        TreeMap<String, Object> treeMap = new TreeMap<>();
        treeMap.put("goods_id", outId);
        //删除下架
        String response = niGuangAttr.post("sold_notice", treeMap, salePartner);
        if (StringUtils.equals(JSONObject.parseObject(response).getString("code"), "1000")) {
            //添加
            result.put("code", 200);
        } else {
            result.put("code", 500);
        }
        artRecMapper.updateArtRec(artRecs.get(0));
        result.put("res", response);
        result.put("msg", JSONObject.parseObject(response).getString("msg"));
        return AjaxResult.success(result);
    }

    private TreeMap<String, Object> builderParam(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        String substring = salePartner.getSaleName().substring(salePartner.getSaleName().indexOf("_") + 1);
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        // 商品请求参数
        TreeMap<String, Object> action = new TreeMap<>();
        // 商品基础信息
        TreeMap<String, Object> goods_info = new TreeMap<>();
        // 商品图片
        goods_info.put("goods_banner", goodsInfoUtils.getGoodsInfoImageList(goodsInfoAndAttrValueDto));
        // 实名类型:0.无；1.成长守护平台有二次更改实名机会；2.号是我实名没有二次更改实名机会；3.号是逆光人脸包赔买的；4.号不是我实名没有二次更改实名机会
        goods_info.put("certification_type", 0);
        // 是否指定购买
        goods_info.put("is_once", 0);
        // 指定购买密码
        goods_info.put("once_password", "123456");
        // 商品名称
        goods_info.put("goods_name", removeNotSupportedStr(goodsInfoAndAttrValueDto.getTitle(), ATTR_VALUE));
        // 商品价格,1-99999
        goods_info.put("goods_price", goodsInfoAndAttrValueDto.getPrice());
        // 游戏id
        goods_info.put("game_id", interAttr.getInterGameId());
        // 商品数量，1-99999
        goods_info.put("goods_stock", 1);
        // 商品类型
        goods_info.put("game_type", "成品号");
        // 是否允许还价 0 不允许 1 允许
        goods_info.put("is_bargain", 1);
        // 将商品基础信息添加到请求参数中
        action.put("goods_info", goods_info);


        // 商品属性结构 List
        List<TreeMap<String, Object>> attr_obj_list = new ArrayList<>();
        // 属性匹配
        Property(goodsInfoAndAttrValueDto, interAttr, attr_obj_list);
        action.put("attr_obj", attr_obj_list);


        // 商品保密信息 list
        List<TreeMap<String, Object>> attr_account_list = new ArrayList<>();
        // 商品保密 map
        TreeMap<String, Object> attr_account_map = new TreeMap<>();
        attr_account_map.put("attr_name", "游戏账号");
        attr_account_map.put("attr_value", goodsInfoAndAttrValueDto.getAccountNumber());
        attr_account_list.add(attr_account_map);

        TreeMap<String, Object> attr_phone_map = new TreeMap<>();
        attr_phone_map.put("attr_name", "联系电话");
        attr_phone_map.put("attr_value", salePartner.getThreeOther());
        attr_account_list.add(attr_phone_map);

        if (goodsInfoAndAttrValueDto.getGameName().equals("王者荣耀")) {
            // 王者营地
            TreeMap<String, Object> attr_account_yingdi = new TreeMap<>();
            attr_account_yingdi.put("attr_name", "王者营地ID");
            attr_account_yingdi.put("attr_value", goodsInfoAndAttrValueDto.getF17());
            attr_account_list.add(attr_account_yingdi);
        }
        action.put("attr_password_obj", attr_account_list);


        // 商户信息
        TreeMap<String, Object> cooperative_info = new TreeMap<>();
        // 商户对应的手机号
        cooperative_info.put("phone", salePartner.getThreeOther());
        // 将商户信息添加到请求参数中
        action.put("cooperative_info", cooperative_info);


        return action;
    }


    private void Property(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, InterAttr interAttr, List<TreeMap<String, Object>> attr_obj_list) {
        builderServerAttr(goodsInfoAndAttrValueDto, attr_obj_list, interAttr.getServer());
        //开始匹配单选属性
        builderRadioAttr(goodsInfoAndAttrValueDto, attr_obj_list, interAttr.getRadioAttr());
        //开始匹配多选属性
        builderCheckAttr(goodsInfoAndAttrValueDto, attr_obj_list, interAttr.getCheckAttr());
        //开始匹配输入框属性
        builderInputAttr(goodsInfoAndAttrValueDto, attr_obj_list, interAttr.getInputAttr());
    }

    /**
     * 开始匹配单选属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param serverAttr
     */
    private void builderServerAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<TreeMap<String, Object>> attr_obj_list, String serverAttr) {

        try {
            JSONObject jsonObject = JSONObject.parseObject(serverAttr);
            //开始匹配一级区服
            String interServeReqs = jsonObject.getString("interServeReqs");
            List<String> qufuList = new ArrayList<>();
            if (Objects.equals(goodsInfoAndAttrValueDto.getGameType(), "0")) {
                if (Objects.nonNull(goodsInfoAndAttrValueDto.getSystemId())) {
                    qufuList.add(goodsInfoAndAttrValueDto.getSystemId().toString());
                }
                if (Objects.nonNull(goodsInfoAndAttrValueDto.getCarrierId())) {
                    qufuList.add(goodsInfoAndAttrValueDto.getCarrierId().toString());
                }
            }
            if (Objects.nonNull(goodsInfoAndAttrValueDto.getGroupId())) {
                qufuList.add(goodsInfoAndAttrValueDto.getGroupId().toString());
            }
            if (Objects.nonNull(goodsInfoAndAttrValueDto.getServerId())) {
                qufuList.add(goodsInfoAndAttrValueDto.getServerId().toString());
            }

            List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);
            String gameName = goodsInfoAndAttrValueDto.getGameName();
            for (JSONObject group : oneServer) {
                String ydServeId = group.getString("YdServeId");
                List<String> ydServeIdList = new ArrayList<>();
                if (StringUtils.isNotEmpty(ydServeId)) {
                    ydServeIdList = Arrays.asList(ydServeId.split(", "));
                }
                if (group.getString("YdServeId").contains(",") && !gameName.equals("穿越火线")) {
                    String[] serveIds = group.getString("YdServeId").replace(" ", "").split(",");
                    if (qufuList.contains(serveIds[0]) && qufuList.contains(serveIds[1])) {
                        TreeMap<String, Object> serverMap = new TreeMap<>();
                        serverMap.put("attr_name", group.getString("InterServeId").split("&")[1]);
                        serverMap.put("attr_value", group.getString("InterServeName"));
                        attr_obj_list.add(serverMap);
                        break;
                    }

                } else if (qufuList.stream().anyMatch(ydServeIdList::contains)) {
                    TreeMap<String, Object> serverMap = new TreeMap<>();
                    serverMap.put("attr_name", group.getString("InterServeId").split("&")[1]);
                    serverMap.put("attr_value", group.getString("InterServeName"));
                    attr_obj_list.add(serverMap);
                    //获取下级数据
                    String interServerResStr = group.getString("interServerRes");
                    if (StringUtils.isNotEmpty(interServerResStr)) {
                        List<JSONObject> jsonObjectList = JSONObject.parseArray(interServerResStr, JSONObject.class);
                        for (JSONObject json : jsonObjectList) {
                            String jsonYdServeId = json.getString("YdServeId");
                            List<String> jsonYdServeIdList = new ArrayList<>();
                            if (StringUtils.isNotEmpty(jsonYdServeId)) {
                                jsonYdServeIdList = Arrays.asList(jsonYdServeId.split(", "));
                            }
                            if (qufuList.stream().anyMatch(jsonYdServeIdList::contains)) {
                                TreeMap<String, Object> jsonServerMap = new TreeMap<>();
                                jsonServerMap.put("attr_name", json.getString("InterServeId").split("&")[1]);
                                jsonServerMap.put("attr_value", json.getString("InterServeName"));
                                attr_obj_list.add(jsonServerMap);
                                if (goodsInfoAndAttrValueDto.getGameName().equals("光遇")) {
                                    TreeMap<String, Object> lastJsonServerMap = new TreeMap<>();
                                    lastJsonServerMap.put("attr_name", "所属服务器");
                                    if (json.getString("InterServeName").equals("网易官服")) {
                                        lastJsonServerMap.put("attr_value", "安卓官服");
                                    } else if (json.getString("InterServeName").equals("其它渠道")) {
                                        lastJsonServerMap.put("attr_value", "安卓渠道服");
                                    } else if (json.getString("InterServeName").equals("网易账号")) {
                                        lastJsonServerMap.put("attr_value", "苹果官服");
                                    }
                                    attr_obj_list.add(lastJsonServerMap);
                                }
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        } catch (Exception e) {
            throw new GlobalException("区服匹配失败");
        }
    }

    private void builderRadioAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<TreeMap<String, Object>> attr_obj_list, String radioAttr) {
        //========================开始匹配单选属性=====================//
        JSONObject jsonObject = JSONObject.parseObject(radioAttr);
        String interRadioReqs = jsonObject.getString("interRadioReqs");
        List<JSONObject> jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {
            throw new GlobalException("接口单选属性暂未配置");
        }
        /**
         * 1、先查询自定义中所有单选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        gameAttrTemplate.setFieldType("radio");
        List<GameAttrTemplate> gameAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();

        List<JSONObject> defObjects = new ArrayList<>();
        defObjects.addAll(jsonObjects);
        for (JSONObject object : jsonObjects) {
            for (GameAttrTemplate attrTemplate : gameAttrTemplates) {
                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        //一级属性
                        //注意 YdConfigId 对应的是 f字段对应的 id
                        if (Objects.equals(object.getLong("YdConfigId"), attrTemplate.getId())) {
                            String id = object.getString("id");
                            defObjects.remove(object);//如果匹配上，则去掉
                            String interRadioReqs1 = object.getString("interRadioReqs");
                            List<JSONObject> jsonObjects2 = JSONObject.parseArray(interRadioReqs1, JSONObject.class);
                            declaredField.setAccessible(true);
                            try {
                                //获取选择的值 例如  实名中的可二次
                                Object o = declaredField.get(goodsInfoAndAttrValueDto);
                                String oneValue = String.valueOf(o);
                                List<JSONObject> twoAttr = jsonObjects2.stream().filter(j -> StringUtils.isNotEmpty(j.getString("YdConfigId"))?j.getString("YdConfigId").contains(oneValue):"1".contains(oneValue)).collect(Collectors.toList());
                                String defaultValue = object.getString("defaultValue");
                                if (org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                    if (StringUtils.isNotEmpty(defaultValue)) {
                                        TreeMap<String, Object> radio_map = new TreeMap<>();
                                        radio_map.put("attr_name", object.getString("InterServeId"));
                                        radio_map.put("attr_value", object.getString("InterServeName"));
                                        radio_map.put("is_title", 1);
                                        radio_map.put("type", "radio");
                                        attr_obj_list.add(radio_map);
                                    }
                                }

                                if (!org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                    JSONObject jsonObject2 = twoAttr.get(0);

                                    TreeMap<String, Object> radio_map = new TreeMap<>();
                                    radio_map.put("attr_name", object.getString("InterConfigName"));
                                    radio_map.put("attr_value", jsonObject2.getString("InterConfigName"));
                                    radio_map.put("is_title", 1);
                                    radio_map.put("type", "radio");
                                    attr_obj_list.add(radio_map);
                                }
                            } catch (Exception e) {
                                throw new GlobalException("单选项" + attrTemplate.getFieldName() + "匹配错误");
                            }
                        }
                    }
                }
            }
        }
        //开始匹配默认值
        for (JSONObject object : defObjects) {
            interRadioReqs = object.getString("interRadioReqs");
            jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
            for (JSONObject objects : jsonObjects) {
                if (StringUtils.isNotEmpty(objects.getString("defaultValue"))) {
                    TreeMap<String, Object> radio_map = new TreeMap<>();
                    radio_map.put("attr_name", object.getString("name"));
                    radio_map.put("attr_value", objects.getString("name"));
                    radio_map.put("is_title", 1);
                    radio_map.put("type", "radio");
                    attr_obj_list.add(radio_map);
                }
            }
        }
        //========================单选属性匹配结束=====================//
    }

    private void builderCheckAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<TreeMap<String, Object>> attr_obj_list, String checkAttr) {
        /**
         * 1、先查询自定义中所有多选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();
        JSONObject checkAttrJson = JSONObject.parseObject(checkAttr);
        String interCheckBoxReqs = checkAttrJson.getString("interCheckBoxReqs");
        List<JSONObject> checkJsonList = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(checkAttrJson)) {
            throw new GlobalException("接口多选属性暂未配置");
        }

        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        List<String> types = new ArrayList<>();
        types.add("checkbox");
        types.add("boxradio");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> checkAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        for (JSONObject object : checkJsonList) {
            for (GameAttrTemplate attrTemplate : checkAttrTemplates) {

                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        declaredField.setAccessible(true);

                        Object o = null;
                        try {
                            o = declaredField.get(goodsInfoAndAttrValueDto);
                        } catch (IllegalAccessException e) {
                            throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
                        }
                        String oneValue = String.valueOf(o);
                        String interCheckBoxReqsStr = object.getString("interCheckBoxReqs");
                        //开始匹配二级属性
                        List<JSONObject> jsonObjects2 = JSONObject.parseArray(interCheckBoxReqsStr, JSONObject.class);
                        /**
                         * 咱们的f字段  多选项都是用空格 隔开，用,号切割数组，跟匹配的数据匹配、匹配上说明此账号勾选的有此属性
                         */
                        String pf_ids = "";
                        for (JSONObject jsonObject1 : jsonObjects2) {
                            String[] split1 = oneValue.split(",");
                            String ydConfigId1 = jsonObject1.getString("YdConfigId");
                            for (String s : split1) {
                                if (Objects.equals(s, ydConfigId1)) {
                                    String InterConfigId2 = jsonObject1.getString("name");
                                    pf_ids += "," + InterConfigId2;
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(pf_ids)) {
                            pf_ids = pf_ids.substring(1);
                            TreeMap<String, Object> checkbox_map = new TreeMap<>();
                            checkbox_map.put("attr_name", object.getString("name"));
                            checkbox_map.put("attr_value", Arrays.asList(pf_ids.split(",")));
                            checkbox_map.put("is_title", 1);
                            checkbox_map.put("type", "select");
                            attr_obj_list.add(checkbox_map);
                        }
                    }
                }
            }
        }

        //开始匹配默认值
        for (JSONObject object : checkJsonList) {
            if(StringUtils.isNotEmpty(object.getString("defaultValue"))) {
                ArrayList<Object> objects1 = new ArrayList<>();
                String interCheckBoxReqs2 = object.getString("interCheckBoxReqs");
                List<JSONObject> jsonObjects = JSONObject.parseArray(interCheckBoxReqs2, JSONObject.class);
                for (JSONObject objects : jsonObjects) {
                    if (StringUtils.isNotEmpty(objects.getString("defaultValue"))) {
                        objects1.add(objects.getString("name"));
                        TreeMap<String, Object> radio_map = new TreeMap<>();
                        radio_map.put("attr_name", object.getString("name"));
                        radio_map.put("attr_value", objects1);
                        radio_map.put("is_title", 1);
                        radio_map.put("type", "select");
                        attr_obj_list.add(radio_map);
                    }
                }
            }
        }
    }

    /**
     * 开始匹配输入框属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param inputAttr
     */
    private void builderInputAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, List<TreeMap<String, Object>> attr_obj_list, String inputAttr) {
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();

        //========================开始匹配输入框=====================//
        JSONObject jsonObject1 = JSONObject.parseObject(inputAttr);
        String inputJson = jsonObject1.getString("inputJson");
        List<JSONObject> jsonObjects1 = JSONObject.parseArray(inputJson, JSONObject.class);
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        ArrayList types = new ArrayList<>();
        types.add("number");
        types.add("input");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> inputTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        List<JSONObject> defInputObjects = new ArrayList<>();
        defInputObjects.addAll(jsonObjects1);
        for (JSONObject object : jsonObjects1) {
            for (GameAttrTemplate inputTemplate : inputTemplates) {
                String aavName = inputTemplate.getAavName();
                if (Objects.equals(object.getString("aavName"), inputTemplate.getAavName())) {
                    defInputObjects.remove(object);
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (Objects.equals(aavName, name)) {
                            declaredField.setAccessible(true);
                            Object o = null;
                            try {
                                o = declaredField.get(goodsInfoAndAttrValueDto);
                            } catch (IllegalAccessException e) {
                                throw new GlobalException("3属性参数错误，请匹配后重新配置");
                            }
                            if (Objects.isNull(o)) {
                                String fieldType = inputTemplate.getFieldType();
                                if (fieldType.equals("number")) {
                                    o = 0;
                                }
                            }
                            if (ObjectUtil.isEmpty(o)) o = 0;
                            String oneValue = String.valueOf(o);
                            if (StringUtils.isNotEmpty(oneValue)) {
                                TreeMap<String, Object> input_map = new TreeMap<>();
                                input_map.put("attr_name", object.getString("interName"));
                                input_map.put("attr_value", oneValue);
                                input_map.put("is_title", 1);
                                input_map.put("type", "input_num");
                                attr_obj_list.add(input_map);
                            }
                        }
                    }
                }

            }
        }
        //开始匹配默认值
        for (JSONObject object : defInputObjects) {
            String defaultValue = object.getString("defaultValue");
            if (StringUtils.isNotEmpty(defaultValue)) {
                TreeMap<String, Object> input_map = new TreeMap<>();
                input_map.put("attr_name", object.getString("name"));
                input_map.put("attr_value", "0");
                input_map.put("is_title", 1);
                input_map.put("type", "input_num");
                attr_obj_list.add(input_map);
            }
        }
    }

    /**
     * 移除不支持字符
     *
     * @param value 原字符
     * @return 处理之后的字符
     */
    private static String removeNotSupportedStr(String value, String valueType) {
        // 移除空格、点和#
        if (valueType.equals(ATTR_NAME))
            value = value.replaceAll("[\\s.#]", "");
        if (valueType.equals(ATTR_VALUE))
            value = value.replaceAll("[*]", "");
        return value;
    }

    public static void main(String[] args) {
        String str = "ccc*cfdjoag*";
        System.out.println(removeNotSupportedStr(str, ATTR_VALUE));
    }
}
